package com.panshi.controller;

import com.github.pagehelper.PageInfo;
import com.panshi.domain.*;
import com.panshi.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class TableController {

    // 搜索
    @Autowired
    private ISearchService iSearchService;

    // 收藏
    @Autowired
    private ICollectService iCollextService;

    // 动态
    @Autowired
    private IDynamicService iDynamicService;

    // 问答/文章
    @Autowired
    private IEssayService iEssayService;

    // 回答
    @Autowired
    private IAnswerService iAnswerService;

    // 提问
    @Autowired
    private IMatterService iMatterService;

    /**
     * 因为使用了分页插件，所以无论查询结果有没有都会有分页效果
     * @param keyword
     * @param page
     * @param pageSize
     * @return
     */

    @ApiOperation("关键字搜索")
    @GetMapping("/search/{keyword}")
    public PageInfo<Search> queryKeyword(@PathVariable(value = "keyword") String keyword,
                                         @RequestParam(value="page",defaultValue="1")Integer page,
                                         @RequestParam(value="pageSize",defaultValue="10")Integer pageSize) {

        System.out.println(keyword + "==================");

        System.out.println(keyword + "++++==================");

        PageInfo<Search> personPageInfo = iSearchService.queryListSearch(keyword, page, pageSize);
        PageInfo<Search> pageInfo= personPageInfo;

        return pageInfo;
    }

    // 动态
    @ApiOperation("动态")
    @GetMapping("/dynamic/{userId}")
    public List<Dynamic> queryDynamic(@PathVariable(value = "userId") long userId) {
        List<Dynamic> dynamicList = iDynamicService.queryListDynamic(userId);
        System.out.println(dynamicList.size());

        // 通过用户id（userId），用于存储传进集合
        List<Essay> essayList = new ArrayList<>();
        List<Answer> answerList = new ArrayList<>();
        List<Matter> matterList = new ArrayList<>();

        // 遍历 List<Dynamic> 里面的数据判断 “类型” 进行查询相关信息
        for (Dynamic dynamic: dynamicList  ) {

            if ("1".equals(dynamic.getDynamicType())) {
                //拿到 dynamic.getDynamicTypeId() 去资讯表根据这个id查单条数据；
                // essayList.add(查询返回对象);
                essayList = iEssayService.queryDynamicIdEssay(userId);
                dynamic.setEssayList(essayList);
            }else if ("2".equals(dynamic.getDynamicType())) {
                //拿到 dynamic.getDynamicTypeId() 去资讯表根据这个id查单条数据；
                // essayList.add(查询返回对象);
                answerList = iAnswerService.queryUserIdAnswer(userId);
                dynamic.setAnswerList(answerList);
            }else if ("3".equals(dynamic.getDynamicType())) {
                //拿到 dynamic.getDynamicTypeId() 去资讯表根据这个id查单条数据；
                // matterList.add(查询返回对象);
                matterList = iMatterService.queryUserIdMatter(userId);
                dynamic.setMatterList(matterList);
            }
        }
        System.out.println(dynamicList + "\t\n");
        return dynamicList;
    }

    // 查询问答
    @ApiOperation("查询问答")
    @GetMapping("/queryEssay/{essayId}")
    public Essay queryEssay(@PathVariable(value = "essayId") long essayId) {
        return iEssayService.queryEssayById(essayId);
    }

    // 查询收藏
    @ApiOperation("查询收藏")
    @GetMapping("/queryCollect/{userId}")
    public List<Collect> queryCollect(@PathVariable(value = "userId") long userId) {
        List<Collect> collectList = iCollextService.queryListCollect(userId);
        System.out.println(collectList + "\t\n");
        return collectList;
    }

    // 增加收藏
    @ApiOperation("增加收藏")
    @GetMapping("/addCollect")
    public void addCollect(@PathVariable(value = "collect") Collect collect) {
        iCollextService.addCollect(collect);
    }

    // 删除收藏
    @ApiOperation("删除收藏")


    @GetMapping("/deleteCollect/{id}")// 收藏id
    public void deleteCollect(@PathVariable(value = "id") long id) {

        iCollextService.deleteCollect(id);
    }

}
